package com.kid.keep.service.impl;

import cn.hutool.core.lang.Assert;
import com.kid.keep.bo.MessageBO;
import com.kid.keep.dao.message.MessageRepository;
import com.kid.keep.dto.MessageDTO;
import com.kid.keep.enums.MessageStatusEnum;
import com.kid.keep.mapper.IMessageCovertMapper;
import com.kid.keep.po.Message;
import com.kid.keep.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 消息
 * @Author JOHN
 * @Create 2020-01-13
 * @Since 1.0.0
 */
@Service
public class MessageService implements IMessageService {
    private MessageRepository messageRepository;
    private IMessageCovertMapper messageCovertMapper;

    @Autowired
    public void setMessageCovertMapper(IMessageCovertMapper messageCovertMapper) {
        this.messageCovertMapper = messageCovertMapper;
    }

    @Autowired
    public MessageService(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }

    @Override
    public Integer countNewMessages(Long creatorId) {
        return messageRepository.countByStatusAndCreatorId(MessageStatusEnum.UN_READE.getValue(), creatorId);
    }

    @Override
    public List<MessageDTO> listNewMessagesByType(Integer type,Long creatorId) {
        List<MessageBO> messages=messageRepository.findByTypeAndStatusAndCreatorId(type,MessageStatusEnum.UN_READE.getValue(),creatorId);
        return messageCovertMapper.toDTO(messages);
    }

    @Override
    public void readMessage(Long messageId, Long creatorId) {
        Message message=new Message();
        message.setStatus(MessageStatusEnum.READE_ED.getValue());
        messageRepository.updateStatusByIdAndCreatorId(message,messageId,creatorId);
    }

    @Override
    public void readMessage(Long creatorId) {
        Message message=new Message();
        message.setStatus(MessageStatusEnum.READE_ED.getValue());
        messageRepository.updateAllStatusByStatusAndCreatorId(message,MessageStatusEnum.UN_READE.getValue(),creatorId);
    }
}
